
var AccountController = function($scope, $http, $modal, $log){
	
	$http.defaults.headers.post["Content-Type"] = "application/json";
	var urlBase="http://localhost:8080/project";
	
	$scope.items = [];
	
	$scope.getListAccount = function(){
		$scope.resetError();
		$http.get(urlBase + '/account/getlist').success(function(respone){
			$scope.listAccount = respone;
		}).error(function(){
			$scope.setError("Can't get list account");
		});
	}
	
	$scope.viewSuccess= function(id){
		$scope.resetError();
		$http.post(urlBase + '/account/get/' + id).success(function(respone){
			 var modalInstance = $modal.open({
			      templateUrl: 'account.html',
			      controller: $scope.ModalInstanceCtrl,
			      resolve: {
			        items: function () {
			          return respone;
			        },
			        $rootScope: function(){
			        	return $scope;
			        }
			 
			      }
			 });
		}).error(function(){
			$scope.setError("Can't get account");
		});
	};
	
	$scope.setError = function(message){
		$scope.error = true;
		$scope.errorMessage = message;
	}
	
	$scope.resetError = function(){
		$scope.error = false;
		$scope.errorMessage = '';
	}
	
	$scope.getListAccount();
	
	$scope.ModalInstanceCtrl = function ($scope, $http, $modalInstance, items, $location, $rootScope) {

		$http.defaults.headers.post["Content-Type"] = "application/json";
		var urlBase="http://localhost:8080/project";
		
		  $scope.items = items;
		  
		  $scope.updateAccount = function () {
		    $modalInstance.close();
		    $http.post(urlBase + '/account/update', $scope.items).success(function(){
		    	$rootScope.getListAccount();
		    	$location.path('/account');
		    }).error(function(){
		    });
		  };

		  $scope.cancel = function () {
		    $modalInstance.dismiss('cancel');
		  };
		  
	}
	
}
